<?php

/**
 *  @FileComment            [货品列表控制器]
 *  @ProjectName             项目名称：shopFrame
 *  @FileName                   文件名称：GoodsListController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-13 8:59:38
 *  *
 */
class GoodsListController extends CommonController {

        public $model, $model2;

        public function __init() {
                parent::__init();
                $this->model = K('GoodsList');
                $this->model2 = K('GoodsAttrTypeAttrView');
        }

        public function index() {
                //显示商品列表信息.货品信息
                if (IS_POST) {
                        if ($this->model->create()) {
                                $this->model->add_data();
                        } else {
                                $this->error($this->model->error);
                        }
                }
                $gid = Q('get.gid', 0, 'intval');
                $standname = $this->model2->get_all_data('goods_gid=' . $gid . ' and class=1 and is_select=1', 'taname');
                $standval = $this->model2->get_all_data('goods_gid=' . $gid . ' and class=1 and is_select=1');
                //显示该商品规格名称
                $this->assign('standname', $standname);
                //显示该商品规格的值
                $this->assign('standval', $standval);
                $goodsList = K('GoodsGoodsListView')->get_data($gid);
                $tempArr = array();
                foreach ($goodsList as $v) {
                        $v['goodsAttr'] = explode(',', $v['combattrid']);
                        $tempArr[] = $v;
                }
                //有什么更好的办法
                foreach ($tempArr as $k => $value) {
                        foreach ($value['goodsAttr'] as $key => $v) {
                                $tempArr[$k]['ganame'][$key] = K('GoodsAttr')->get_one_data($gid, $v);
                        }
                }
                $this->assign('goodslist', $tempArr);
                $this->display();
        }

        public function check() {
                //验证组合字段，如果存在，则该货品已存在，拒绝添加
                $comb = Q('post.combattrid');
                $comb = implode(',', $comb);
                $data = K('GoodsList')->where(array('combattrid' => $comb))->find();
                if (!empty($data)) {
                        echo 0;
                } else {
                        echo 1;
                }die;
        }

        public function del() {
                //删除货品
                $gid = Q('get.gid', 0, 'intval');
                $glid = Q('get.glid', 0, 'intval');
                $this->model->del_data($glid);
                $this->success('删除货品成功！', U('index', array('gid' => $gid)));
        }

        public function edit() {
                //编辑货品，只能编辑库存和货号
                $model = K('GoodsListEdit');
                if (IS_POST) {
                        $glid = Q('post.glid', 0, 'intval');
                        $gid = Q('post.goods_gid', 0, 'intval');
                        if ($model->create()) {
                                $model->update_data($glid);
                                $this->success('修改货品成功！', U('index', array('gid' => $gid)));
                        } else {
                                $this->error($model->error);
                        }
                }
                $glid = Q('get.glid', 0, 'intval');
                $gid = Q('get.gid', 0, 'intval');
                $standname = K('GoodsAttrTypeAttrView')->get_data($gid, 1, 'taname');
                $nowGlist = $this->model->get_one_data($glid);
                $tempArr = explode(',', $nowGlist['combattrid']);
                $comb = array();
                foreach ($tempArr as $v) {
                        $comb[] = K('GoodsAttr')->get_one_data($gid, $v);
                }
                $this->assign('nowGlist', $nowGlist);
                $this->assign('comb', $comb);
                $this->assign('standname', $standname);
                $this->display();
        }

        public function setdefault() {
                //设置前台进入内容页默认显示的货号
                $gid = Q('get.gid', 0, 'intval');
                $glid = Q('get.glid', 0, 'intval');
                $data = $this->model->get_one_data($glid);
                $newData = array();
                $newData['comb'] = $data['combattrid'];
                K('Goods')->update_comb($gid, $newData);
                $this->success('设置成功！', U('index', array('gid' => $gid)));
        }

}
